package com.yqyd.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.yqyd.hy.domain.HyGoods;
import com.yqyd.hy.domain.HyGoodsPriceRecord;
import com.yqyd.hy.service.IHyGoodsPriceRecordService;
import com.yqyd.hy.service.IHyGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yqyd.common.utils.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author Dhq
 */
@Component("PriceTask")
public class PriceTask {


    private static final Integer STATUS = 1 ;


    @Resource
    private IHyGoodsService goodsService;
    @Resource
    private IHyGoodsPriceRecordService recordService;

    /**
     * 记录价格
     */
    public void RecordPrice(){
        // 昨天日期
        String yesterday = DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd") ;
        // 所有商品
        List<HyGoods> goodsList = goodsService.selectHyGoodsList(new HyGoods().setStatus(STATUS));
        goodsList.forEach(goods -> {
            // 商品价格
            BigDecimal price = goods.getNewsPrice();
            // 历史价格
            goods.setHistoryPrice(price);
            // 更新
            goodsService.updateHyGoods(goods);

            recordService.insertHyGoodsPriceRecord(
                    new HyGoodsPriceRecord()
                            .setGoodsId(goods.getId())
                            .setPrice(price)
                            .setDay(yesterday));
        });
    }

}
